<?php

class Controller_MyQEE__Category__Index extends Controller_Admin
{
    /**
     * 栏目页列表
     */
    public function action_default()
    {
        # 检查权限
        if ( !$this->session()->member()->perm()->is_own('cms.category.view') )
        {
            $this->message('您不具备相应的权限');
        }
        $view = new View('admin/cms/category/list');
        $view->render();
    }

    /**
     * 添加栏目控制器方法
     */
    public function action_add()
    {
        # 检查权限
        if ( !$this->session()->member()->perm()->is_own('cms.category.add') )
        {
            $this->message('您不具备相应的权限');
        }
        if ( HttpIO::METHOD == 'POST' )
        {
            $this->save($_POST['data']);
            return ;
        }

        $this->show_form();
    }

    /**
     * 添加栏目控制器方法
     */
    public function action_edit($cid=0)
    {
        # 检查权限
        if ( !$this->session()->member()->perm()->is_own('cms.category.edit') )
        {
            $this->message('您不具备相应的权限');
        }
        $cid = (int)$cid;
        if ( !$cid>0 )
        {
            $this->message('缺少参数',-1);
        }

        if ( HttpIO::METHOD == 'POST' )
        {
            $_POST['id'] = $cid;
            $this->save($_POST['data']);
            return ;
        }

        $this->show_form($cid);
    }

    protected function show_form($cid=0)
    {
        if ( $cid>0 )
        {


        }
        $data = array();
        View::set_global('data',$data);
        $view = new View('admin/cms/category/form');
        $view->render();
    }

    /**
     * 保存栏目信息
     *
     * @param array $data
     */
    protected function save($data)
    {
        try
        {
            $model_category = new Model_Category_Save();
            if ($data['id'])
            {
                $model_category->edit($data);
            }
            else
            {
                $model_category->add($data);
            }
            $this->message('保存成功',1);
        }
        catch (Exception $e)
        {
            $this->message($e->getMessage(),-1);
        }
    }

}